<?php

if (isset($_REQUEST['view_internships'])) {
   //user clicked on a view button
   //get the organizationID
   $orgID = $_REQUEST['viewCompany'];
   displayInternships($orgID);
} else {

   include 'db-control/dbOrganization.php';
   $result = dbOrganization::getAllOrganizations();

   if (!$result

      )die("Could not complete query...");
   $numRows = mysql_num_rows($result);
   echo "<table width='100%' cellspacing=0><tr><td width='10%'></td><td><b>Name</b></td><td><b>Address</b></td><td><b>City</b></td><td><b>State</b></td><td><b>Zip</b></td><td width='30%'><b>Description</b></td></tr>";
   echo "<tr><td colspan=7><hr /></td></tr>";

   for ($x = 0; $x < $numRows; $x++) {
      $row = mysql_fetch_assoc($result);
      $id = $row['organizationID'];
      $name = $row['organizationName'];
      $address = $row['address'];
      $city = $row['city'];
      $state = strtoupper($row['stateName']);
      $zip = $row['zip'];
      $desc = $row['companyDescription'];
      if (strlen($desc) > 50) {
         $desc = substr($desc, 0, -(strlen($desc) - 50)) . "...";
      }
      if ($x % 2 == 0) {
         echo "<tr>";
      } else {
         echo "<tr bgcolor='#FBFBFB'>";
      }
      echo "<td align='center' valign='middle'><br /><form action='index.php' method='get'><input type='hidden' name='viewCompany' value='$id'><input type='submit' value='Internships' class='submit' name='view_internships' /></form></td>";
      echo "<td>$name</td>";
      echo "<td>$address</td>";
      echo "<td>$city</td>";
      echo "<td>$state</td>";
      echo "<td>$zip</td>";
      echo "<td>$desc</td>";
      echo "</tr>";
   }
   echo "</table>";
}

function displayInternships($orgID) {
   connectToDatabase();
   $category = $_SESSION['category'];
   $query = "SELECT o.city, o.stateName, o.organizationName, io.* FROM opportunity io, organization o WHERE io.organizationID=$orgID AND io.categories LIKE '%$category%' ORDER BY io.dateAdded DESC";

   $result = mysql_query($query);
   if (!$result
      )die("Could not complete query: " . mysql_error());

   $numRows = mysql_num_rows($result);
   //back button
   echo "<script type='text/javascript'>function viewCompanies(){ window.location='index.php?repCompanies=true';}</script>";
   echo "<div align='left' width='100%'><input style='cursor:pointer;' type='button' onclick='viewCompanies();' value='Go Back'/></div>";
   echo "<table width='100%' cellspacing=0><tr><td width='10%'></td><td><b>Company</b></td><td><b>Location</b></td><td><b>Position</b></td><td width='10%'><b>Positions Available</b></td><td><b>Pay Rate</b></td><td><b>Semester</b></td><td width='15%'><b>Date Added</b> (Y-M-D)</td></tr>";
   echo "<tr><td colspan=8><hr /></td></tr>";
   for ($x = 0; $x < $numRows; $x++) {
      $row = mysql_fetch_assoc($result);
      $id = $row['opportunityID'];
      $city = $row['city'];
      $state = strtoupper($row['stateName']);
      $title = $row['title'];
      $name = $row['organizationName'];
      $numPositions = $row['numPositionsAvailable'];
      $payRate = $row['payRate'];
      $semester = $row['semester'];
      $dateAdded = $row['dateAdded'];
      $dateAdded = substr($dateAdded, 0, 10);
      if ($x % 2 == 0) {
         echo "<tr>";
      } else {
         echo "<tr bgcolor='#FBFBFB'>";
      }
      echo "<td align='center' valign='middle'><br /><form action='index.php' method='get'><input type='hidden' name='internship_id' value='$id'><input type='submit' value='View' class='submit' name='view_internship' /></form></td>";
      echo "<td>$name</td>";
      echo "<td>$city, " . $state . "</td>";
      echo "<td>$title</td>";
      echo "<td>$numPositions</td>";
      echo "<td>$payRate</td>";
      echo "<td>$semester</td>";
      echo "<td>$dateAdded</td>";
      echo "</tr>";
   }
   echo "</table>";
}

?>
